package com.gistone.commis.api.restController;

import com.gistone.commis.constant.Status;
import com.gistone.commis.dto.EditorResult;
import com.gistone.commis.dto.FieldError;
import com.gistone.commis.dto.Result;
import com.gistone.commis.entity.*;
import com.gistone.commis.mapper.*;
import com.gistone.commis.query.QueryPerson;
import com.gistone.commis.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

/**
 * Created by wangfan on 2017/5/5.
 */
@RestController("apiSWageController")
@RequestMapping("api/sWage")
public class SWageController {

    private static final Logger logger = LoggerFactory.getLogger(SWageController.class);

    @Value("${app.wageDate}")
    private Integer wageIntDate;
    @Autowired
    private SWageMapper sWageMapper;

    @Autowired
    private FWageMapper fWageMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PersonMapper personMapper;

    @Autowired
    private FixedItemMapper fixedItemMapper;

    @Autowired
    private PersonCcMapper personCcMapper;
    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private AttendanceNotifyMapper attendanceNotifyMapper;

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private PositionTypeMapper positionTypeMapper;

    @RequestMapping(method = RequestMethod.GET, value = "/export")
    public Result export(HttpServletResponse response, @RequestParam Integer year, @RequestParam Integer month) throws IOException {
        String fileName = "返聘人员工资-" + year + "-" + month + ".xls";

        String sheetName = "返聘人员工资";//sheet名
        String[] title = new String[]{"人员Id（不可更改）", "姓名", "养老", "医疗", "失业", "公积金", "个人所得税"};//标题


        QueryPerson qp = new QueryPerson();
        qp.setIsDelete(0);


        List<Person> list = personMapper.selectSelective(qp);//内容list


        String[][] values = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            values[i] = new String[title.length];
            //将对象内容转换成string
            Person person = list.get(i);
            values[i][0] = person.getPersonId() + "";
            values[i][1] = person.getPersonName() + "";
            values[i][2] = person.getPension() + "";
            values[i][3] = person.getMedicare() + "";
            values[i][4] = person.getUnemployment() + "";
            values[i][5] = person.getProvidentFund() + "";
            values[i][6] = person.getIncomeTax() + "";
//            values[i][6] = person.getBackPay()+"";
//            values[i][7] = person.getSupPf()+"";
//            values[i][8] = person.getSupI()+"";

        }

        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, values, null);

        //将文件存到指定位置
        try {
            ResponseHeaderUtil.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(0);
    }


    //修改逻辑，哪些项可以修改
    @Transactional
    @RequestMapping(method = RequestMethod.PUT, value = "/edit")
    public Object edit(@RequestParam("id") String houseIdStr, HttpServletRequest request, HttpServletResponse response) {
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("userInfo");
        String[] arr = houseIdStr.split(",");
        EditorResult<Property> propertyEditorResult = new EditorResult<>();
        List<Property> data = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            int propertyId = Integer.valueOf(arr[i]);
            Property property = new Property();
            property.setPropertyId(propertyId);
            List<FieldError> fieldErrorList = new ArrayList<>();
            property.setApplyUserId(user.getUserId());
//            getProperty(request, "data[" + propertyId + "][", property, fieldErrorList);
//            if (fieldErrorList.size() > 0) {
//                logger.error("字段错误");
//                propertyEditorResult.setFieldErrors(fieldErrorList);
//                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
//                return propertyEditorResult;
//            }
//            int no;
//            try {
//                no = propertyMapper.updateByPrimaryKeySelective(property);
//            } catch (Exception e) {
//                logger.error("字段错误");
//                fieldErrorList.add(new FieldError("propertyCode", "资产编号不能重复"));
//                propertyEditorResult.setFieldErrors(fieldErrorList);
//                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
//                return propertyEditorResult;
//            }
//            if (no > 0) {
//                System.out.println("property---" + property);
//                Property p = propertyMapper.selectByPrimaryKey(propertyId);
//
//                data.add(p);
//                PropertyLog propertyLog = new PropertyLog(p, new Date(), user.getUserId());
//
//                if (propertyLogMapper.insertSelective(propertyLog) > 0) {
//                    logger.info("保存资产日志信息成功");
//                } else {
//                    logger.info("保存资产日志信息失败");
//                    response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
//
//                }
//
//
//            } else {
//                response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
//            }


        }
        propertyEditorResult.setData(data);
        return propertyEditorResult;


    }

    @Transactional
    @RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
    public DataTablePageUtil<SWage> pageDatatables(HttpServletRequest request, SWage sWage) {
//		HttpSession httpSession = request.getSession();
//		User user = (User) httpSession.getAttribute("userInfo");
//		logger.info("用户信息：" + user.toString());
//		int community_id = user.getCommunity().getCommunityId();
//		person.setCommunityId(community_id);


        DataTablePageUtil<SWage> dataTable = new DataTablePageUtil<>(request);

        PageHelper.startPage(dataTable.getPage_num(), 10);
        List<SWage> list = sWageMapper.selectSelective(sWage);

        PageInfo<SWage> pageInfo = new PageInfo<>(list);
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
//        dataTable.setOptions(os);
        dataTable.setRecordsTotal((int) pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面

        return dataTable;

    }

    /**
     * 工资计算
     *
     * @param request
     * @return
     */
    @Transactional
    @RequestMapping(method = RequestMethod.GET, value = "/calculate")
    public Result calculate(HttpServletRequest request) throws Exception {

        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("userInfo");
        if (null == user) {
            logger.info("登录用户已经失效，请重新登录！");
            return new Result(Status.NOUSER, "登录用户已经失效，请重新登录！", null);
        }
        logger.info("用户信息：" + user.toString());
        Result result = new Result();
        result.setStatus(0);

        Date d = new Date();
        Calendar now = Calendar.getInstance();
        int nowYear = now.get(Calendar.YEAR);
        int nowMonth = now.get(Calendar.MONTH) + 1;


        String wageStrDate = nowYear + "-" + nowMonth + "-" + wageIntDate;
        Calendar wd = Calendar.getInstance();
        wd.setTime(DateUtil.StrToDate1(wageStrDate));

        SWage s = new SWage();
        s.setWageMonth(nowMonth);
        s.setWageYear(nowYear);

        List<SWage> list = sWageMapper.selectSelective(s);


        if (list.size() > 0) {

            for (SWage sWage : list) {
                sWageMapper.deleteByPrimaryKey(sWage.getSwageId());

            }
//            logger.info("已经存在当月工资数据!");
//            return new Result(Status.REPEAT, "已经存在当月工资数据!", null);
        }
        FWage f = new FWage();
        f.setWageMonth(nowMonth);
        f.setWageYear(nowYear);
        List<FWage> flist = fWageMapper.selectSelective(f);

        if (flist.size() > 0) {

            for (FWage fWage : flist) {
                fWageMapper.deleteByPrimaryKey(fWage.getFwageId());

            }
//            logger.info("已经存在当月工资数据!");
//            return new Result(Status.REPEAT, "已经存在当月工资数据!", null);
        }
        //计算工资

            List<Community> communityList = communityMapper.selectCommunitysWithout1();

            //先判定每个社区的考勤数据是否上传

            List<String> communityNames = new ArrayList<>();
            for (Community community : communityList) {

                int commmunityId = community.getCommunityId();

                if (attendanceNotifyMapper.selectByCommunityAndYearAndMonthAndFlag(commmunityId, nowYear, nowMonth,0).size()==1) {
                    communityNames.add(community.getCommunityName());
                } else if (attendanceNotifyMapper.selectByCommunityAndYearAndMonth(commmunityId, nowYear, nowMonth).size()==0) {
                    communityNames.add(community.getCommunityName());
                    AttendanceNotify attendanceNotify = new AttendanceNotify();
                    attendanceNotify.setCommunityId(commmunityId);
                    attendanceNotify.setYear(nowYear);
                    attendanceNotify.setMonth(nowMonth);
                    attendanceNotify.setFlag(0);
                    attendanceNotifyMapper.insertSelective(attendanceNotify);

                }


            }

            if(communityNames.size()>0){
                return  new Result(Status.ATTENDANCE,"考勤数据未上传,已经给相关社区发送通知，等上传数据和才能计算工资",communityNames);
            }


            QueryPerson queryPerson = new QueryPerson();
//            person.setCommunityId(user.getCommunityId());
            queryPerson.setIsDelete(0);
            queryPerson.setLeaveStatus(0);
            queryPerson.setNoCommunityId(1);
            List<Person> persons = personMapper.selectSelective(queryPerson);//先得到当前社区的人员信息
            FixedItem fixedItem = fixedItemMapper.selectLatest();
            for (Person p : persons) {//开始计算每个人的工资

                if (p.getEnlistType().equals("社招")) {
                    SWage sWage = new SWage();
                    sWage.setPersonId(p.getPersonId());
                    sWage.setCommunityId(p.getCommunityId());
                    sWage.setDepartId(p.getDepartId());
                    sWage.setUserId(user.getUserId());
                    sWage.setPersonName(p.getPersonName());
                    System.out.println("----：" + p.getPersonName() + ":----:" + p.getPositionId());
                    PositionType positionType = positionTypeMapper.selectByPrimaryKey(p.getPositionId());
                    sWage.setBasicSalary(positionType.getPositionJwage());//基本工资
                    sWage.setQdnxSub(positionType.getPositionQdnx());//起点职务年限补贴
                    sWage.setZwnxbt(p.getZwnxbt());//职务年限补贴

                    sWage.setDsznSub(0.0);//独生子女费 固定项维护
                    if (p.getIsDszn() == 1) {
                        int compare = new Date().compareTo(p.getDsznDeadline()); //满足条件当前日期小于截止日期就取值
                        if (compare < 0) {
                            sWage.setDsznSub(fixedItem.getDsznSub());//独生子女补贴多少钱
                        }

                    }
//                    sWage.setPostSalary(positionType.getPositionGwjt());//岗位工资 没有
                    sWage.setEspSub(positionType.getPositionGwjt());//岗位津贴

                    //计算规则


                    List<PersonCc> personCcList = personCcMapper.selectSelective(null);

                    Map<Integer, Double> map = new HashMap();//
                    for (PersonCc cc : personCcList) {

                        map.put(cc.getCcId(), cc.getCcWage());

                    }
                    int ccId = p.getPersonCc().getCcId();
                    if (ccId == CcId.NOTHING) {//以到岗日期为基准
                        System.out.println("workDate----" + p.getSWorkDate());
                        Calendar i = Calendar.getInstance();
                        i.setTime(p.getSWorkDate());

                        int dif = i.compareTo(wd);
                        System.out.println("----" + dif);

                        int difYear = dif / 365 / 24 / 60 / 60;
                        System.out.println(difYear);


                        if (difYear >= 0 && difYear < 3) {//不满三年
                            sWage.setTecSalary(map.get(CcId.GZY_ONE));//就以工作员二级计算工资
                        } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                            sWage.setTecSalary(map.get(CcId.GZY_TWO));//就以工作员二级计算工资
                        } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                            sWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                        } else if (difYear >= 11) {
                            sWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                        }


                    } else {
                        Calendar i = Calendar.getInstance();
                        if (null == p.getCcDate()) {
                            i.setTime(new Date());
//                            return  new Result(Status.ERROR,"有证人员的领证日期不能为空，请检查","");
                        } else {
                            i.setTime(p.getCcDate());
                        }

                        int dif = i.compareTo(wd);
                        System.out.println("----" + dif);


                        int difYear = dif / 365 / 24 / 60 / 60;
                        System.out.println(difYear);

                        if (ccId == CcId.GZY_ONE) {//如果当前是工作员一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GZY_ONE));//就以工作员一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                sWage.setTecSalary(map.get(CcId.GZY_TWO));//就以工作员二级计算工资
                            } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                                sWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                            } else if (difYear >= 11) {
                                sWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                            }

                        } else if (ccId == CcId.GZY_TWO) {//如果当前是工作员二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GZY_TWO));//就以工作员二级计算工资
                            } else if (difYear >= 3 && difYear < 8) {//满三年且不到六年
                                sWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                            } else if (difYear >= 8) {
                                sWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                            }

                        } else if (ccId == CcId.GZY_THREE) {//如果当前是工作员三级
                            if (difYear >= 0 && difYear < 5) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                            } else if (difYear >= 5) {
                                sWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                            }

                        } else if (ccId == CcId.GZY_FOUR) {//如果当前是工作员调整档
                            sWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                        }


                        if (ccId == CcId.ZLGZS_ONE) {//如果当前是助理工作师一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.ZLGZS_ONE));//就以助理工作师一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                sWage.setTecSalary(map.get(CcId.ZLGZS_TWO));//就以助理工作师二级计算工资
                            } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                                sWage.setTecSalary(map.get(CcId.ZLGZS_THREE));//就以助理工作师三级计算工资
                            } else if (difYear >= 11) {
                                sWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                            }

                        } else if (ccId == CcId.ZLGZS_TWO) {//如果当前是助理工作师二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.ZLGZS_TWO));//就以助理工作师二级计算工资
                            } else if (difYear >= 3 && difYear < 8) {//满三年且不到六年
                                sWage.setTecSalary(map.get(CcId.ZLGZS_THREE));//就以助理工作师三级计算工资
                            } else if (difYear >= 8) {
                                sWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                            }

                        } else if (ccId == CcId.ZLGZS_THREE) {//如果当前是助理工作师三级
                            if (difYear >= 0 && difYear < 5) {//不满三年
                                sWage.setTecSalary(map.get(CcId.ZLGZS_THREE));//就以助理工作师三级计算工资
                            } else if (difYear >= 5) {
                                sWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                            }

                        } else if (ccId == CcId.ZLGZS_FOUR) {//如果当前是助理工作师调整档
                            sWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                        }


                        if (ccId == CcId.GZS_ONE) {//如果当前是工作师一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GZS_ONE));//就以工作师一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                sWage.setTecSalary(map.get(CcId.GZS_TWO));//就以工作师二级计算工资
                            } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                                sWage.setTecSalary(map.get(CcId.GZS_THREE));//就以工作师三级计算工资
                            } else if (difYear >= 11) {
                                sWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                            }

                        } else if (ccId == CcId.GZS_TWO) {//如果当前是工作师二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GZS_TWO));//就以工作师二级计算工资
                            } else if (difYear >= 3 && difYear < 8) {//满三年且不到六年
                                sWage.setTecSalary(map.get(CcId.GZS_THREE));//就以工作师三级计算工资
                            } else if (difYear >= 8) {
                                sWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                            }

                        } else if (ccId == CcId.GZS_THREE) {//如果当前是工作师三级
                            if (difYear >= 0 && difYear < 5) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GZS_THREE));//就以工作师三级计算工资
                            } else if (difYear >= 5) {
                                sWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                            }

                        } else if (ccId == CcId.GZS_FOUR) {//如果当前是工作师调整档
                            sWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                        }


                        if (ccId == CcId.GJGZS_ONE) {//如果当前是高级工作师一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GJGZS_ONE));//就以高级工作师一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                sWage.setTecSalary(map.get(CcId.GJGZS_TWO));//就以高级工作师二级计算工资
                            } else if (difYear >= 6) {//满六年
                                sWage.setTecSalary(map.get(CcId.GJGZS_THREE));//就以高级工作师三级计算工资
                            }

                        } else if (ccId == CcId.GJGZS_TWO) {//如果当前是高级工作师二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                sWage.setTecSalary(map.get(CcId.GJGZS_TWO));//就以高级工作师二级计算工资
                            } else if (difYear >= 3) {//满三年
                                sWage.setTecSalary(map.get(CcId.GJGZS_THREE));//就以高级工作师三级计算工资
                            }

                        } else if (ccId == CcId.GJGZS_THREE) {//如果当前是高级工作师三级
                            sWage.setTecSalary(map.get(CcId.GJGZS_THREE));//就以高级工作师三级计算工资


                        }

                    }

//                    sWage.setTecSalary(personCc.getCcWage());//专业技术等级工资


                    sWage.setWorkSub(fixedItem.getWorkSub());//工作津贴  固定项
                    sWage.setExistSub(fixedItem.getExistSub());//生活补贴 固定项
                    sWage.setMonthBonus(fixedItem.getMonthBonus());//月奖金 固定项

                    sWage.setPerBonus(fixedItem.getPerBonus());//绩效奖金 固定项


                    sWage.setBackPay(p.getBackPay());//补发 人员信息
                    double sum_wage =
                            sWage.getBasicSalary()+sWage.getZwnxbt()
                                    + sWage.getQdnxSub() + sWage.getDsznSub()
                                   /* + sWage.getPostSalary()*/ + sWage.getEspSub()
                                    + sWage.getTecSalary() + sWage.getWorkSub()
                                    + sWage.getExistSub() + sWage.getMonthBonus()
                                    + sWage.getPerBonus() + sWage.getBackPay();

                    sWage.setSumWage(sum_wage);// 应发合计

                    double leaveAffairs = 0;
                    Attendance attendance = attendanceMapper.selectByPersonAndYearAndMonth(p.getPersonId(), nowYear, nowMonth);
                    if (null != attendance) {//没数据改怎么处理
                        leaveAffairs = attendance.getLeaveAffairs();
//                        attendance.get
                    } else {

                        System.out.println(p.toString());
                        throw new RuntimeException("考勤数据未全部上传！");


//                        return  new Result(Status.ERROR,"考勤数据未全部上传！","");
                    }


                    double a = leaveAffairs * (positionType.getPositionQdnx() / 22);
                    double b = leaveAffairs * (fixedItem.getMonthBonus() / 22);//月奖金部分
                    double c = leaveAffairs * (fixedItem.getPerBonus() / 22);
                    if (leaveAffairs > 3) {//事假超过三天
                        b = fixedItem.getMonthBonus();

                    }
                    if (attendance.getLeaveSick() > 5) {//病假超过五天
                        b = fixedItem.getMonthBonus();
                    }
                    if (attendance.getLeaveAffairs() + attendance.getLeaveSick() > 6) {//病事假累计超过六天的
                        b = fixedItem.getMonthBonus();
                    }


                    sWage.setLeaveDed(a + b + c);//事假扣款

                    sWage.setPension(p.getPension());//养老
                    sWage.setMedicare(p.getMedicare());//医疗
                    sWage.setUnemployment(p.getUnemployment());//失业
                    sWage.setProvidentFund(p.getProvidentFund());//公积金
                    sWage.setIncomeTax(p.getIncomeTax());//个人所得税

                    sWage.setSupPf(p.getSupPf());//补扣公积金 人员
                    sWage.setSupI(p.getSupI());//补扣三险  人员
                    double sum_sup = sWage.getLeaveDed() + sWage.getPension() + sWage.getMedicare()
                            + sWage.getUnemployment() + sWage.getProvidentFund() + sWage.getIncomeTax()
                            + sWage.getSupPf() + sWage.getSupI();
                    sWage.setSumSup(sum_sup);// 补扣合计

                    sWage.setSumSWage(sum_wage - sum_sup);//实发合计


                    sWage.setWageMonth(nowMonth);
                    sWage.setWageYear(nowYear);

                    sWage.setRegisterTime(new Date());


                    sWageMapper.insertSelective(sWage);
                } else {


                    FWage fWage = new FWage();
                    fWage.setPersonId(p.getPersonId());
                    fWage.setCommunityId(p.getCommunityId());
                    fWage.setDepartId(p.getDepartId());
                    fWage.setUserId(user.getUserId());
                    fWage.setPersonName(p.getPersonName());
                    System.out.println("----：" + p.getPersonName() + ":----:" + p.getPositionId());
                    PositionType positionType = positionTypeMapper.selectByPrimaryKey(p.getPositionId());

                    fWage.setPostSalary(positionType.getPositionGwjt());
                    fWage.setEspSub(positionType.getPositionGwjt());//岗位津贴
                    PersonCc personCc = personCcMapper.selectByPrimaryKey(p.getCcId());

                    //计算规则


                    List<PersonCc> personCcList = personCcMapper.selectSelective(null);

                    Map<Integer, Double> map = new HashMap();//
                    for (PersonCc cc : personCcList) {

                        map.put(cc.getCcId(), cc.getCcWage());

                    }
                    int ccId = p.getPersonCc().getCcId();
                    if (ccId == CcId.NOTHING) {//以到岗日期为基准
                        System.out.println("workDate----" + p.getSWorkDate());
                        Calendar i = Calendar.getInstance();
                        i.setTime(p.getSWorkDate());

                        int dif = i.compareTo(wd);
                        System.out.println("----" + dif);

                        int difYear = dif / 365 / 24 / 60 / 60;
                        System.out.println(difYear);


                        if (difYear >= 0 && difYear < 3) {//不满三年
                            fWage.setTecSalary(map.get(CcId.GZY_ONE));//就以工作员二级计算工资
                        } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                            fWage.setTecSalary(map.get(CcId.GZY_TWO));//就以工作员二级计算工资
                        } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                            fWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                        } else if (difYear >= 11) {
                            fWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                        }


                    } else {
                        Calendar i = Calendar.getInstance();
                        if (null == p.getCcDate()) {
                            i.setTime(new Date());
//                            return  new Result(Status.ERROR,"有证人员的领证日期不能为空，请检查","");
                        } else {
                            i.setTime(p.getCcDate());
                        }
                        int dif = i.compareTo(wd);
                        System.out.println("----" + dif);


                        int difYear = dif / 365 / 24 / 60 / 60;
                        System.out.println(difYear);

                        if (ccId == CcId.GZY_ONE) {//如果当前是工作员一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GZY_ONE));//就以工作员一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                fWage.setTecSalary(map.get(CcId.GZY_TWO));//就以工作员二级计算工资
                            } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                                fWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                            } else if (difYear >= 11) {
                                fWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                            }

                        } else if (ccId == CcId.GZY_TWO) {//如果当前是工作员二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GZY_TWO));//就以工作员二级计算工资
                            } else if (difYear >= 3 && difYear < 8) {//满三年且不到六年
                                fWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                            } else if (difYear >= 8) {
                                fWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                            }

                        } else if (ccId == CcId.GZY_THREE) {//如果当前是工作员三级
                            if (difYear >= 0 && difYear < 5) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GZY_THREE));//就以工作员三级计算工资
                            } else if (difYear >= 5) {
                                fWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                            }

                        } else if (ccId == CcId.GZY_FOUR) {//如果当前是工作员调整档
                            fWage.setTecSalary(map.get(CcId.GZY_FOUR));//就以工作员调整档计算工资
                        }


                        if (ccId == CcId.ZLGZS_ONE) {//如果当前是助理工作师一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.ZLGZS_ONE));//就以助理工作师一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                fWage.setTecSalary(map.get(CcId.ZLGZS_TWO));//就以助理工作师二级计算工资
                            } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                                fWage.setTecSalary(map.get(CcId.ZLGZS_THREE));//就以助理工作师三级计算工资
                            } else if (difYear >= 11) {
                                fWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                            }

                        } else if (ccId == CcId.ZLGZS_TWO) {//如果当前是助理工作师二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.ZLGZS_TWO));//就以助理工作师二级计算工资
                            } else if (difYear >= 3 && difYear < 8) {//满三年且不到六年
                                fWage.setTecSalary(map.get(CcId.ZLGZS_THREE));//就以助理工作师三级计算工资
                            } else if (difYear >= 8) {
                                fWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                            }

                        } else if (ccId == CcId.ZLGZS_THREE) {//如果当前是助理工作师三级
                            if (difYear >= 0 && difYear < 5) {//不满三年
                                fWage.setTecSalary(map.get(CcId.ZLGZS_THREE));//就以助理工作师三级计算工资
                            } else if (difYear >= 5) {
                                fWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                            }

                        } else if (ccId == CcId.ZLGZS_FOUR) {//如果当前是助理工作师调整档
                            fWage.setTecSalary(map.get(CcId.ZLGZS_FOUR));//就以助理工作师调整档计算工资
                        }


                        if (ccId == CcId.GZS_ONE) {//如果当前是工作师一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GZS_ONE));//就以工作师一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                fWage.setTecSalary(map.get(CcId.GZS_TWO));//就以工作师二级计算工资
                            } else if (difYear >= 6 && difYear < 11) {//满六年且不到11年
                                fWage.setTecSalary(map.get(CcId.GZS_THREE));//就以工作师三级计算工资
                            } else if (difYear >= 11) {
                                fWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                            }

                        } else if (ccId == CcId.GZS_TWO) {//如果当前是工作师二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GZS_TWO));//就以工作师二级计算工资
                            } else if (difYear >= 3 && difYear < 8) {//满三年且不到六年
                                fWage.setTecSalary(map.get(CcId.GZS_THREE));//就以工作师三级计算工资
                            } else if (difYear >= 8) {
                                fWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                            }

                        } else if (ccId == CcId.GZS_THREE) {//如果当前是工作师三级
                            if (difYear >= 0 && difYear < 5) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GZS_THREE));//就以工作师三级计算工资
                            } else if (difYear >= 5) {
                                fWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                            }

                        } else if (ccId == CcId.GZS_FOUR) {//如果当前是工作师调整档
                            fWage.setTecSalary(map.get(CcId.GZS_FOUR));//就以工作师调整档计算工资
                        }


                        if (ccId == CcId.GJGZS_ONE) {//如果当前是高级工作师一级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GJGZS_ONE));//就以高级工作师一级计算工资
                            } else if (difYear >= 3 && difYear < 6) {//满三年且不到六年
                                fWage.setTecSalary(map.get(CcId.GJGZS_TWO));//就以高级工作师二级计算工资
                            } else if (difYear >= 6) {//满六年
                                fWage.setTecSalary(map.get(CcId.GJGZS_THREE));//就以高级工作师三级计算工资
                            }

                        } else if (ccId == CcId.GJGZS_TWO) {//如果当前是高级工作师二级
                            if (difYear >= 0 && difYear < 3) {//不满三年
                                fWage.setTecSalary(map.get(CcId.GJGZS_TWO));//就以高级工作师二级计算工资
                            } else if (difYear >= 3) {//满三年
                                fWage.setTecSalary(map.get(CcId.GJGZS_THREE));//就以高级工作师三级计算工资
                            }

                        } else if (ccId == CcId.GJGZS_THREE) {//如果当前是高级工作师三级
                            fWage.setTecSalary(map.get(CcId.GJGZS_THREE));//就以高级工作师三级计算工资


                        }

                    }


                    fWage.setWorkSub(fixedItem.getWorkSub());//工作津贴
                    fWage.setExistSub(fixedItem.getExistSub());//生活补贴
                    fWage.setMonthBonus(fixedItem.getMonthBonus());//月奖金
                    fWage.setPerBonus(fixedItem.getPerBonus());//绩效奖金

                    fWage.setBackPay(p.getBackPay());
                    double wage =

                            +fWage.getPostSalary() + fWage.getEspSub()
                                    + fWage.getTecSalary() + fWage.getWorkSub()
                                    + fWage.getExistSub() + fWage.getMonthBonus()
                                    + fWage.getPerBonus() + fWage.getBackPay();


                    double leaveAffairs = 0;
                    Attendance attendance = attendanceMapper.selectByPersonAndYearAndMonth(p.getPersonId(), nowYear, nowMonth);
                    if (null != attendance) {//没数据改怎么处理
                        leaveAffairs = attendance.getLeaveAffairs();
//                        attendance.get
                    }


                    double a = leaveAffairs * (positionType.getPositionQdnx() / 22);
                    double b = leaveAffairs * (fixedItem.getMonthBonus() / 22);//月奖金部分
                    double c = leaveAffairs * (fixedItem.getPerBonus() / 22);

                    if (attendance.getLeaveAffairs() > 3) {//事假超过三天
                        a = fixedItem.getMonthBonus();

                    }
                    if (attendance.getLeaveSick() > 5) {//病假超过五天
                        b = fixedItem.getMonthBonus();
                    }
                    if (attendance.getLeaveAffairs() + attendance.getLeaveSick() > 6) {//病事假累计超过六天的
                        c = fixedItem.getMonthBonus();
                    }


                    fWage.setLeaveDed(a + b + c);//事假扣款


                    fWage.setSumWage(wage - fWage.getLeaveDed());//实发合计


                    fWage.setWageMonth(nowMonth);
                    fWage.setWageYear(nowYear);

                    fWage.setRegisterTime(new Date());
                    fWageMapper.insertSelective(fWage);

                }


            }

            return new Result(Status.SUCCESS, "", null);




    }


}
